package com.tianhui.consignortwofei.mvp.ui.activity.hetong;

import android.text.TextUtils;
import android.widget.AutoCompleteTextView;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.other.CommonEvent;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseDataListActivity;
import com.tianhui.consignortwofei.mvp.model.UserInfoModel;
import com.tianhui.consignortwofei.mvp.model.hetong.DuiXIangListBean;
import com.tianhui.consignortwofei.mvp.ui.activity.hetong.adapter.DuiXiangOneListAdapter;
import com.tianhui.consignortwofei.other.EventWhat;


import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.inter.OnItemClickListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2021/8/10
 * @description
 */
public class DuiXiangSelectListActivity extends BaseDataListActivity {

    @BindView(R.id.layout_search_editText)
    AutoCompleteTextView searchEditText;




    private UserInfoModel driverModel;

    private String mSearchKey;


    @Override
    public int getLayoutId() {
        return R.layout.activity_duixiang_list;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }


    @Override
    public RecyclerArrayAdapter createAdapter() {
        return new DuiXiangOneListAdapter(getContext());
    }

//    private void setResult(DriverInfo driverInfo) {
//        Intent intent = new Intent();
//        intent.putExtra(KeyValue.KEY_NAME, driverInfo.drivername);
//        intent.putExtra(KeyValue.KEY_PHONE, driverInfo.drivertel);
//        intent.putExtra(KeyValue.KEY_VEHICLE_NUMBER, driverInfo.defaultvehiclenum);
//        setResult(KeyValue.KEY_RESULT_CODE, intent);
//        finish();
//    }

    @Override
    public void initListener() {
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                DuiXIangListBean data = (DuiXIangListBean)mAdapter.getAllData().get(position);
                data.setShowChild(!data.isShowChild());
                mAdapter.notifyItemChanged(position,data);
//                EventBus.getDefault().post(new CommonEvent(EventWhat.EVENT_RETURN_FENLEI.getValue(),data.getName(),data.getCategoryId()+""));
//                DriverInfo driverInfo = (DriverInfo) mAdapter.getAllData().get(position);
//                if (driverInfo != null && !TextUtils.isEmpty(driverInfo.drivertel)) {
//                    setResult(driverInfo);
//                }
//                FindCarDeliveryInfo.CarRecords carRecords = allList.get(position);
//                Gson gson=new Gson();
//                String json = gson.toJson(carRecords);
//
//                Bundle bundle=new Bundle();
//                bundle.putString("erpdetails",json);
//                openActivity(FindCarLiteDeliverActivity.class,bundle);



            }
        });
    }

    @Override
    public void requestDataList(int page, boolean isDialog) {
        mSearchKey = searchEditText.getText().toString();
//        mSearchKey="202212";

        if (checkSearchKey(mSearchKey)) {
            getShopDetailSort(mSearchKey, isDialog,page);
        } else {
            stopRefresh();
        }
    }

    @Override
    public void initData() {
        setTitle("分类");

        driverModel = new UserInfoModel();
        searchEditText.setHint("请输分类名称");
        setPageInfo(1, 1);
        getShopDetailSort("", true,1);
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @OnClick(R.id.layout_search_textView)
    public void searchDriverDetail() {

        mSearchKey = searchEditText.getText().toString();
        if (checkSearchKey(mSearchKey)) {
            setRefresh(true);
            getShopDetailSort(mSearchKey, true,1);
        }
    }

//    @Override
//    public void onEvent(CommonEvent event) {
//        super.onEvent(event);
//        switch (EventWhat.valueOf(event.getWhat())) {
//            case EVENT_RETURN_FENLEI:
//                finish();
//                break;
//        }
//    }


    @Override
    public void onEvent(CommonEvent event) {
        super.onEvent(event);
        switch (EventWhat.valueOf(event.getWhat())) {
            case EVENT_REFRESH_DUIXIANG_LIST:
              finish();
                break;

        }
    }

    private boolean checkSearchKey(String searchKey) {
        if (TextUtils.isEmpty(searchKey)) {
            return false;
//            showToast("请输入正确的单号");
        } else  {
            return true;
        }
//        return false;


//        if (TextUtils.isEmpty(searchKey)) {
//            showEmptyView();
//        } else if (CheckUtil.isMobileNO(mSearchKey)) {
//            return true;
//        } else {
//            showToast("请输入正确的单号");
//        }

    }


//    private List<FindCarDeliveryInfo.CarRecords> allList=new ArrayList<>();
//    public void getSearchCarList(String code, boolean isDialog,int page) {
//        if (null==allList){
//            allList=new ArrayList<>();
//        }
//
//        String consignorName = SharedPreferenceManager.getInstance().getConsignorName();
//        Map<String, String> map = new HashMap<>();
//        map.put("code", code);
//        map.put("page", page + "");
//        map.put("limit", "15");
//        map.put("offset ", "0");
//
//        map.put("personname", consignorName);
//
//        map.put("sort", "voucherdate");
//        map.put("order", "desc");
//
//
//        //http://124.239.149.36:10611/tianhui/erpware/viewSASaleDelivery
//        if (driverModel == null) {
//
//        }
//
//        driverModel.getSearchCarList(this, map, isDialog, bindLifecycle(), new ObserverResponseListener<CommonResponse<FindCarDeliveryInfo>>() {
//            @Override
//            public void onNext(CommonResponse<FindCarDeliveryInfo> commonResponse) {
//                if (!isFinishing() && !isDestroyed()) {
//                    if (commonResponse.getCode() == 20000) {
//                        allList.clear();
//                        List<FindCarDeliveryInfo.CarRecords> list = commonResponse.getData().getRecords();
////                        List<DriverInfo> list = commonResponse.getData();
////                        List<DriverInfo> resultList = new ArrayList<>();
//
//                        if (list != null && list.size() > 0) {
//                            allList.addAll(list);
//
////                            setPageInfo(1, 1);
////                            for (DriverInfo info : list) {
////                                if (info.drivertel.equals(code)) {
////                                    resultList.add(info);
////                                }
////                            }
//                            if (list.size() > 0) {
//
//                                showDataList(list);
//                            } else {
//                                showEmptyView();
//                            }
//
//                        } else {
//                            showEmptyView();
//                        }
//
//                    } else {
//                        showToast(commonResponse.getMsg());
//                    }
//                }
//            }
//
//
//            @Override
//            public void onError(ExceptionHandle.ResponseThrowable e) {
//                showToast(e.message);
//            }
//        });
//
//    }

    private void getShopDetailSort(String code, boolean isDialog,int page) {
//        ShopFenleiSendBean sendBean=new ShopFenleiSendBean();
//        sendBean.setCategoryId("");
//        sendBean.setPageIndex(page+"");
//        sendBean.setPageSize("10");
//        sendBean.setProductName("");
//        sendBean.setType("sell");

        Map<String, String> map = new HashMap<>();
        map.put("name",code);

        driverModel.getShenHeList(this, map, true, this.bindLifecycle(),
                new ObserverResponseListener<CommonResponse<List<DuiXIangListBean>>>() {
                    @Override
                    public void onNext(CommonResponse<List<DuiXIangListBean>> commonResponse) {
                        if (!isFinishing() && !isDestroyed()) {
                            if (commonResponse.getCode() == 20000) {
                                List<DuiXIangListBean> list = commonResponse.getData();

                                List<DuiXIangListBean> newList =new ArrayList<>();
                                for (int i=0;i<list.size();i++){
                                    if (null!=list.get(i).getChildren()&&list.get(i).getChildren().size()>0){
                                        newList.add(list.get(i));
                                    }
                                }
                                if (newList != null && newList.size() > 0) {
//                                    getLeiXingType(list);
                                    mAdapter.removeAll();
                                    mAdapter.addAll(newList);
                                } else {
                                    showToast("未获取到分类列表信息");
                                }
                            } else {
                                showToast(commonResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {

                        showToast(e.message);
                    }
                });
    }




}